﻿﻿import tkinter as tk
import math
window= tk.Tk()
window.title('Oil calculation')
window.geometry('800x400')

t_10_text=tk.Label(window,text='t10').place(x=20,y=0,anchor='nw')
t_10=tk.Entry(window)
t_10.place(x=100,y=0,anchor='nw')
t_30_text=tk.Label(window,text='t30').place(x=20,y=25,anchor='nw')
t_30=tk.Entry(window)
t_30.place(x=100,y=25,anchor='nw')
t_50_text=tk.Label(window,text='t50').place(x=20,y=50,anchor='nw')
t_50=tk.Entry(window)
t_50.place(x=100,y=50,anchor='nw')
t_70_text=tk.Label(window,text='t70').place(x=20,y=75,anchor='nw')
t_70=tk.Entry(window)
t_70.place(x=100,y=75,anchor='nw')
t_90_text=tk.Label(window,text='t90').place(x=20,y=100,anchor='nw')
t_90=tk.Entry(window)
t_90.place(x=100,y=100,anchor='nw')

d20_text=tk.Label(window,text='d20密度').place(x=20,y=150,anchor='nw')
d20_=tk.Entry(window)
d20_.place(x=100,y=150,anchor='nw')

d152_6_text=tk.Label(window,text='d15.6密度').place(x=20,y=175,anchor='nw')
d152_6_=tk.Entry(window)
d152_6_.place(x=100,y=175,anchor='nw')

TME_text=tk.Label(window,text='中平均沸点tMe').place(x=20,y=200,anchor='nw')
TME_=tk.Entry(window)
TME_.place(x=100,y=200,anchor='nw')

TV_text=tk.Label(window,text='体积平均沸点tv').place(x=20,y=225,anchor='nw')
TV_=tk.Entry(window)
TV_.place(x=100,y=225,anchor='nw')

first_text=tk.Label(window,text='第一关键馏分').place(x=130,y=275,anchor='nw')
first_d20_text=tk.Label(window,text='d20').place(x=20,y=300,anchor='nw')
first_api_text=tk.Label(window,text='api').place(x=20,y=325,anchor='nw')
first_d20_=tk.Entry(window)
first_d20_.place(x=100,y=300,anchor='nw')
first_api_=tk.Entry(window)
first_api_.place(x=100,y=325,anchor='nw')

second_text=tk.Label(window,text='第二关键馏分').place(x=500,y=275,anchor='nw')
second_d20_text=tk.Label(window,text='d20').place(x=390,y=300,anchor='nw')
second_api_text=tk.Label(window,text='api').place(x=390,y=325,anchor='nw')
second_d20_=tk.Entry(window)
second_d20_.place(x=470,y=300,anchor='nw')
second_api_=tk.Entry(window)
second_api_.place(x=470,y=325,anchor='nw')

by_=tk.Label(window,text='yunpeng zhao').place(x=700,y=375,anchor='nw')


def cal():
    t10 = float(t_10.get())
    t30 = float(t_30.get())
    t50 = float(t_50.get())
    t70 = float(t_70.get())
    t90 = float(t_90.get())
    slope = (t90 - t10) / 80

    tv=(t10+t30+t50+t70+t90)/5
    tv_.delete('1.0','end')
    tv_.insert('insert',tv)

    dw=math.exp(-3.64991-0.02706*tv**0.6667+5.16388*slope**0.25)
    tw=tv+dw
    tw_.delete('1.0','end')
    tw_.insert('insert',tw)

    dm=math.exp(-1.15158-0.01181*tv**0.6667+3.70684*slope**0.3333)
    tm=tv-dm
    tm_.delete('1.0', 'end')
    tm_.insert('insert', tm)

    dcu=math.exp(-0.82368-0.089970*tv**0.45+2.45679*slope**0.45)
    tcu = tv - dcu
    tcu_.delete('1.0', 'end')
    tcu_.insert('insert', tcu)

    dme=math.exp(-1.53181-0.0128*tv**0.6667+3.64678*slope**0.3333)
    tme = tv - dme
    tme_.delete('1.0', 'end')
    tme_.insert('insert', tme)

def cal2():
    d20=float(d20_.get())
    d15_6=(1.598-d20)/(176.1-d20)+d20
    d15_6_.delete('1.0', 'end')
    d15_6_.insert('insert',d15_6)

    api=141.5/d15_6-131.5
    api_.delete('1.0', 'end')
    api_.insert('insert', api)

    TME=float(TME_.get())
    d152_6=float(d152_6_.get())
    uopk=1.216*(TME+273)**0.33333/d152_6
    uopk_.delete('1.0', 'end')
    uopk_.insert('insert',uopk)

    TV = float(TV_.get())
    bmci = 48640 / (TV + 273) + 473.7 * d152_6-456.8
    bmci_.delete('1.0', 'end')
    bmci_.insert('insert', bmci)

def cal3():
    first_d20=float(first_d20_.get())
    first_api=float(first_api_.get())
    second_d20 = float(second_d20_.get())
    second_api = float(second_api_.get())
    if first_d20<0.821:
        if first_api>40:
            first = '石蜡基'
        else:
            first = '不知道'
    elif 0.821<first_d20<0.8562:
        if 33<first<40:
            first='中间基'
        else:
            first='不知道'
    elif first_d20>0.8562:
        if first_api<33:
            first='环烷基'
        else:
            first='不知道'
    else:
        first='不知道'
    first_.delete('1.0', 'end')
    first_.insert('insert', first)

    if second_d20<0.8723:
        if second_api>30:
            second = '石蜡基'
        else:
            second = '不知道'
    elif 0.8723<second_d20<0.9305:
        if 20<second<30:
            second='中间基'
        else:
            second='不知道'
    elif second_d20>0.9305:
        if second_api<20:
            second='环烷基'
        else:
            second='不知道'
    else:
        second='不知道'
    second_.delete('1.0', 'end')
    second_.insert('insert', second)

b1=tk.Button(window,text='计算平均沸点',width=12,height=1,command=cal).place(x=650,y=30,anchor='nw')
b2=tk.Button(window,text='计算',width=8,height=1,command=cal2).place(x=650,y=175,anchor='nw')
b2=tk.Button(window,text='计算基属',width=8,height=1,command=cal3).place(x=650,y=300,anchor='nw')

tv_text=tk.Label(window,text='体积平均沸点tv').place(x=340,y=0,anchor='nw')
tv_=tk.Text(window,width=20,height=1.4)
tv_.place(x=450,y=0,anchor='nw')

tw_text=tk.Label(window,text='质量平均沸点tw').place(x=340,y=25,anchor='nw')
tw_=tk.Text(window,width=20,height=1.4)
tw_.place(x=450,y=25,anchor='nw')

tm_text=tk.Label(window,text='实分子平均沸点tm').place(x=340,y=50,anchor='nw')
tm_=tk.Text(window,width=20,height=1.4)
tm_.place(x=450,y=50,anchor='nw')

tcu_text=tk.Label(window,text='立方平均沸点tcu').place(x=340,y=75,anchor='nw')
tcu_=tk.Text(window,width=20,height=1.4)
tcu_.place(x=450,y=75,anchor='nw')

tme_text=tk.Label(window,text='中平均沸点tme').place(x=340,y=100,anchor='nw')
tme_=tk.Text(window,width=20,height=1.4)
tme_.place(x=450,y=100,anchor='nw')

d15_6_text=tk.Label(window,text='d15.6密度').place(x=340,y=150,anchor='nw')
d15_6_=tk.Text(window,width=20,height=1.4)
d15_6_.place(x=450,y=150,anchor='nw')

api_text=tk.Label(window,text='比重指数API°').place(x=340,y=175,anchor='nw')
api_=tk.Text(window,width=20,height=1.4)
api_.place(x=450,y=175,anchor='nw')

uopk_text=tk.Label(window,text='特性因数K').place(x=340,y=200,anchor='nw')
uopk_=tk.Text(window,width=20,height=1.4)
uopk_.place(x=450,y=200,anchor='nw')

bmci_text=tk.Label(window,text='相关指数BMCI').place(x=340,y=225,anchor='nw')
bmci_=tk.Text(window,width=20,height=1.4)
bmci_.place(x=450,y=225,anchor='nw')

first_=tk.Text(window,width=20,height=1.4)
first_.place(x=100,y=350,anchor='nw')

second_=tk.Text(window,width=20,height=1.4)
second_.place(x=470,y=350,anchor='nw')

window.mainloop()